package com.googlecode.jvmpart.support;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.googlecode.jvmpart.proc.JvmProcessException;

public abstract class RegexPartitioner extends AbsPartitioner{
	@Override
	public Map<String, Map<String, Object>> doPartition()
			throws JvmProcessException {
		Matcher m = null;
		m = getPattern().matcher(getContent());
		try {
			int offset = 0;	
			Map<String, Map<String, Object>> params = new HashMap<String, Map<String, Object>>();
			while (m.find()) {
				Map<String,Object> param = match(m, offset);
				params.put(getJvmProcessName()+"_"+offset++, param);
				offset++;
			}
			return params;
		} catch(Exception e) {
			e.printStackTrace();
			throw new JvmProcessException("Regex error", e);
		}		
	}
	public abstract Pattern getPattern();
	public abstract String getContent() throws JvmProcessException;
	public abstract Map<String, Object> match(Matcher m, int offset) throws JvmProcessException;

}
